package com.sinocc.workmanager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.sinocc.entity.QQMailPo;
import com.sinocc.util.HttpClientUtil;
import com.sinocc.util.HttpClientUtil45;
import com.sinocc.util.MapCache;

public class CPCPKgMailCount {
	
	protected MapCache cache = MapCache.single();
	
	/**
	 * 
	 * @param userId 用户邮箱
	 * @param beginDate 查询开始日期：2018-11-06
	 * @param endDate 查询结束日期：2018-11-08
	 * @return
	 */
	private Integer getMessageByUserId(String userId,String beginDate,String endDate) {
		
		Map<String,String> params = new HashMap<String,String>();
		
		params.put("access_token", getTockenByTX());
		params.put("userid", userId);
		params.put("begin_date", beginDate);
		params.put("end_date", endDate);
		
		String url = "https://api.exmail.qq.com/cgi-bin/mail/newcount";
		
		//返回{"errcode":0,"errmsg":"ok","count":4}
		String result = HttpClientUtil.sendByHttpsGet(params, url);
		Gson gs = new Gson();  
		
		QQMailPo qm = gs.fromJson(result, QQMailPo.class);
		
		System.out.print("最终输出：");
		System.out.println(qm.toString());
		
		return qm.getCount();
	}
	
	
	/**
	 * 请求AccessToken
	 */
	private String getTockenByTX() {

		/*
		 * 获取tocken
		 */
		Map<String, String> params = new HashMap<String, String>();
		params.put("corpid", "wm84a53b2aa7bbf588");
		params.put("corpsecret", "GnU0lI3HOLhCiyUGuZXUpMWFA9TJI-lpxBJF-MZgag5RfrmfSbf2nSP4p6FxO9vw");

		String url = "https://api.exmail.qq.com/cgi-bin/gettoken";
		String accessTocken = cache.get("accessTocken");
		
		String time = cache.get("tocken_cache_time");
		
		if(accessTocken != null) {
			
			try {
				//如果时间在区间内
				if(time != null) {
					if(valitTime(time)) {
						return accessTocken;
					}
				}
				
				//如果时间为空或者不在区间内,获取tocken并存储
				return saveMapAndReturn(params, url);
				
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			
		}else {
			accessTocken = saveMapAndReturn(params, url);//HttpClientUtil.sendByHttpsGet(params, url);
		}

		return accessTocken;
	}
	
	/**
	 * 判断当前时间是不是在安全时间区间内
	 * @return
	 * @throws ParseException 
	 */
	private boolean valitTime(String timeZone) throws ParseException {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String times[] = timeZone.split(";");
		long sd = format.parse(times[0]).getTime();
		long ed = format.parse(times[1]).getTime();
		long nowd = new Date().getTime();
		if(nowd>=sd && nowd < ed) 
			return true;
		else
			return false;
		
	}
	
	/**
	 * 保存值并返回tocken
	 * @param params
	 * @param url
	 * @return
	 */
	private String saveMapAndReturn(Map<String,String> params, String url) {
		
		String result = HttpClientUtil.sendByHttpsGet(params, url);
		
		System.out.print("看一下输出,result:");
		System.out.println(result);
		
		Gson gs = new Gson();  
		
		QQMailPo qm = gs.fromJson(result, QQMailPo.class);
		
		System.out.print("看一下对象输出,QQMailPo:");
		System.out.println(qm.toString());
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		Date nd = new Date();
		
		String sd = format.format(nd);
		String ed = format.format(new Date(nd.getTime()+(qm.getExpires_in()*1000)));
		
		cache.set("accessTocken",qm.getAccess_token());
		
		cache.set("tocken_cache_time",sd+";"+ed);
		
		return qm.getAccess_token();
	}
	


}
